/**
 * Tform.java
 * com.eova.widget.tform
 * Copyright (c) 2016, 北京聚智未来科技有限公司版权所有.
 */

package com.eova.widget.tform;

import com.eova.model.MetaField;
import com.eova.model.MetaObject;
import com.eova.service.sm;
import com.eova.widget.WidgetManager;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * TODO(这里用一句话描述这个类的作用)
 * <p>
 * TODO(这里描述这个类补充说明 – 可选)
 *
 * @author gyp
 * @Date 2016年12月23日
 * @Time 下午3:56:54
 */
public class TformController extends Controller {

	public void examine() {
		buildFormData(false);
		render("/eova/widget/form/examine.html");
	}

	private void buildFormData(boolean isEdit) {
		String objectCode = getPara(0);

		Object pkValue = getPara(1);

		MetaObject object = sm.meta.getMeta(objectCode);

		Record record = Db.use(object.getDs()).findById(object.getView(),
				object.getPk(), new Object[] { pkValue });

		for (MetaField ei : object.getFields()) {
			String key = ei.getEn();
			Object value = record.get(key);

			if (value == null) {
				value = "";
			}

			if (!isEdit) {
				ei.set("is_disable", Boolean.valueOf(true));
			}

			ei.put("value", value);
		}

		WidgetManager.buildRef(this, object);

		setAttr("object", object);
		setAttr("fields", object.getFields());
	}

}
